/*
Copyright 2014, KISSY v1.47
MIT Licensed
build time: May 22 12:26
*/
KISSY.add("editor/plugin/table",["editor","./dialog-loader","./contextmenu","./button"],function(i,m){function s(a){function b(a){!(0<e.length)&&a[0].nodeType===n.NodeType.ELEMENT_NODE&&t.test(a.nodeName())&&!a.data("selected_cell")&&(a._4eSetMarker(f,"selected_cell",!0,void 0),e.push(a))}for(var d=a.createBookmarks(),g=a.getRanges(),e=[],f={},c=0;c<g.length;c++){var h=g[c];if(h.collapsed)h=h.getCommonAncestor(),(h=h.closest("td",void 0)||h.closest("th",void 0))&&e.push(h);else{var h=new C(h),k;for(h.guard=
b;k=h.next();)if((k=k.parent())&&t.test(k.nodeName())&&!k.data("selected_cell"))k._4eSetMarker(f,"selected_cell",!0,void 0),e.push(k)}}l.Utils.clearAllMarkers(f);a.selectBookmarks(d);return e}function u(a,b){var d=a.getStartElement().parent("tr");if(d){var g=d.clone(!0);g.insertBefore(d);d=(b?g[0]:d[0]).cells;for(g=0;g<d.length;g++)d[g].innerHTML="",v||(new j(d[g]))._4eAppendBogus(void 0)}}function q(a){var b;if(a instanceof l.Selection){for(var d=s(a),g=d.length,a=[],e,f,c=0;c<g;c++){b=d[c].parent();
var h=b[0].rowIndex;c||(e=h-1);a[h]=b;c===g-1&&(f=h+1)}b=b.parent("table");e=new j(f<b[0].rows.length&&b[0].rows[f]||0<e&&b[0].rows[e]||b[0].parentNode);for(c=a.length;0<=c;c--)a[c]&&q(a[c]);return e}a instanceof j&&(b=a.parent("table"),1===b[0].rows.length?b.remove():a.remove());return 0}function w(a,b){var d=a.getStartElement();if(d=d.closest("td",void 0)||d.closest("th",void 0))for(var g=d.parent("table"),e=d[0].cellIndex,f=0;f<g[0].rows.length;f++){var c=g[0].rows[f];c.cells.length<e+1||(d=new j(c.cells[e].cloneNode(void 0)),
v||d._4eAppendBogus(void 0),c=new j(c.cells[e]),b?d.insertBefore(c):d.insertAfter(c))}}function x(a){var b;if(a instanceof l.Selection){var d=s(a),g,e=[],a=d[0]&&d[0].parent("table"),f,c;f=0;for(c=d.length;f<c;f++)e.push(d[f][0].cellIndex);e.sort();f=1;for(c=e.length;f<c;f++)if(1<e[f]-e[f-1]){b=e[f-1]+1;break}b||(b=0<e[0]?e[0]-1:e[e.length-1]+1);e=a[0].rows;f=0;for(c=e.length;f<c&&!(g=e[f].cells[b]);f++);g=g?new j(g):a.prev();for(b=d.length-1;0<=b;b--)d[b]&&x(d[b]);return g}if(a instanceof j){d=a.parent("table");
if(!d)return null;g=a[0].cellIndex;for(b=d[0].rows.length-1;0<=b;b--)a=new j(d[0].rows[b]),!g&&1===a[0].cells.length?q(a):a[0].cells[g]&&a[0].removeChild(a[0].cells[g])}return null}function y(a,b){var d=new l.Range(a[0].ownerDocument);if(!d.moveToElementEditablePosition(a,b?!0:void 0))d.selectNodeContents(a),d.collapse(b?!1:!0);d.select(!0)}function o(a){var b=(a=a.getSelection())&&a.getStartElement(),d=b&&b.closest("table",void 0);if(d)return a=b.closest(function(a){var b=n.nodeName(a);return d.contains(a)&&
("td"===b||"th"===b)},void 0),b=b.closest(function(a){var b=n.nodeName(a);return d.contains(a)&&"tr"===b},void 0),{table:d,td:a,tr:b}}function p(a){return(a=o(a))&&a.td}function r(a){return(a=o(a))&&a.tr}function z(a){this.config=a||{}}var l=m("editor"),v=11>i.UA.ieMode,C=l.Walker,A=m("./dialog-loader");m("./contextmenu");m("./button");var n=i.DOM,j=i.Node,D=["tr","th","td","tbody","table"],t=/^(?:td|th)$/,B={"\u8868\u683c\u5c5e\u6027":o,"\u5220\u9664\u8868\u683c":p,"\u5220\u9664\u5217":p,"\u5220\u9664\u884c":r,"\u5728\u4e0a\u65b9\u63d2\u5165\u884c":r,"\u5728\u4e0b\u65b9\u63d2\u5165\u884c":r,"\u5728\u5de6\u4fa7\u63d2\u5165\u5217":p,"\u5728\u53f3\u4fa7\u63d2\u5165\u5217":p},E=(6===i.UA.ie?
["table.%2,","table.%2 td, table.%2 th,","{","border : #d3d3d3 1px dotted","}"]:" table.%2,; table.%2 > tr > td,  table.%2 > tr > th,; table.%2 > tbody > tr > td,  table.%2 > tbody > tr > th,; table.%2 > thead > tr > td,  table.%2 > thead > tr > th,; table.%2 > tfoot > tr > td,  table.%2 > tfoot > tr > th;{;border : #d3d3d3 1px dotted;}".split(";")).join("").replace(/%2/g,"ke_show_border"),F={tags:{table:function(a){var b=a.getAttribute("class"),d=parseInt(a.getAttribute("border"),10);if(!d||0>=d)a.setAttribute("class",
i.trim((b||"")+" ke_show_border"))}}},G={tags:{table:function(a){var b=a.getAttribute("class");b&&((b=i.trim(b.replace("ke_show_border","")))?a.setAttribute("class",b):a.removeAttribute("class"))}}};i.augment(z,{pluginRenderUI:function(a){a.addCustomStyle(E);var b=a.htmlDataProcessor,d=b&&b.htmlFilter;(b&&b.dataFilter).addRules(F);d.addRules(G);var g=this,e={"\u8868\u683c\u5c5e\u6027":function(){this.hide();var c=o(a);c&&A.useDialog(a,"table",g.config,{selectedTable:c.table,selectedTd:c.td})},"\u5220\u9664\u8868\u683c":function(){this.hide();
var c=a.getSelection(),b=c&&c.getStartElement();if(b=b&&b.closest("table",void 0)){a.execCommand("save");c.selectElement(b);var d=c.getRanges()[0];d.collapse();c.selectRanges([d]);c=b.parent();1===c[0].childNodes.length&&"body"!==c.nodeName()&&"td"!==c.nodeName()?c.remove():b.remove();a.execCommand("save")}},"\u5220\u9664\u884c ":function(){this.hide();a.execCommand("save");var c=a.getSelection();y(q(c),void 0);a.execCommand("save")},"\u5220\u9664\u5217 ":function(){this.hide();a.execCommand("save");var c=a.getSelection();(c=
x(c))&&y(c,!0);a.execCommand("save")},"\u5728\u4e0a\u65b9\u63d2\u5165\u884c":function(){this.hide();a.execCommand("save");var c=a.getSelection();u(c,!0);a.execCommand("save")},"\u5728\u4e0b\u65b9\u63d2\u5165\u884c":function(){this.hide();a.execCommand("save");var c=a.getSelection();u(c,void 0);a.execCommand("save")},"\u5728\u5de6\u4fa7\u63d2\u5165\u5217":function(){this.hide();a.execCommand("save");var c=a.getSelection();w(c,!0);a.execCommand("save")},"\u5728\u53f3\u4fa7\u63d2\u5165\u5217":function(){this.hide();a.execCommand("save");var c=a.getSelection();w(c,void 0);a.execCommand("save")}},f=[];i.each(e,function(a,
b){f.push({content:b})});a.addContextMenu("table",function(a){if(i.inArray(n.nodeName(a),D))return!0},{width:"120px",children:f,listeners:{click:function(a){a=a.target.get("content");e[a]&&e[a].apply(this)},beforeVisibleChange:function(a){if(a.newVal){var b=this,a=b.get("children"),d=b.get("editor");i.each(a,function(a){var c=a.get("content");!B[c]||B[c].call(b,d)?a.set("disabled",!1):a.set("disabled",!0)})}}}});a.addButton("table",{mode:l.Mode.WYSIWYG_MODE,listeners:{click:function(){A.useDialog(a,
"table",g.config,{selectedTable:0,selectedTd:0})}},tooltip:"\u63d2\u5165\u8868\u683c"})}});return z});
